"""
Case Type   : security-audit
Case Name   : replace包含delete insert操作应该被审计到
Create At   : 2023/06/08
Owner       : @zou_jialiang0501162244
Description :
    1、设置audit_dml_state=1，创建兼容B库
    2、B库建表，利用replace命令添加数据
    3、创建replace相关存储过程
    4、调用存储过程，查看审计记日志
    5、清理环境
Expect      :
    1、成功
    2、执行成功
    3、成功
    4、正确审计到replace操作
History     :
"""

import os
import unittest

from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class TestPrivilege(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.cons = Constant()
        self.db_name = 'db_audit_0010'
        self.table = 't_audit_0010'
        self.proc = 'pro_audit_0010'

    def test_security(self):
        text1 = '-----step1:设置audit_dml_state=1，创建兼容B库;expect:建表成功-----'
        self.log.info(text1)
        sql = f"alter system set audit_dml_state = 1;" \
              f"drop database if exists {self.db_name};" \
              f"create database {self.db_name} dbcompatibility 'B';"
        preset_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(preset_res)
        self.assertIn(self.cons.alter_system_success_msg, preset_res,
                      f"设置audit_dml_state参数失败:{text1}")
        self.assertIn(self.cons.CREATE_DATABASE_SUCCESS, preset_res,
                      f"创建兼容B库失败:{text1}")

        text2 = '-----step2:B库建表，利用replace命令添加数据;expect:执行成功-----'
        self.log.info(text2)
        sql = f'''drop table if exists {self.table} cascade;
        create table {self.table}(uid int primary key, name varchar(50));
        replace into {self.table} values(1, 'dbsec');'''
        sql_res = self.pri_sh.execut_db_sql(sql, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn(self.cons.CREATE_TABLE_SUCCESS, sql_res, f"执行失败:{text2}")
        self.assertIn('REPLACE 0 1', sql_res, f"执行失败: {text2}")

        text3 = '-----step3:创建replace相关存储过程;expect:成功-----'
        self.log.info(text3)
        res = self.pri_sh.execut_db_sql(
            f'''create or replace procedure {self.proc}() as
            begin
            replace into {self.table} values(2, 'ccccc');
            end;''', dbname=self.db_name)
        self.log.info(res)
        self.assertIn(self.cons.CREATE_PROCEDURE_SUCCESS_MSG, res,
                      f"执行失败: {text3}")

        text4 = '-----step4:调用存储过程，查看审计日志;expect:正确审计到replace操作-----'
        self.log.info(text4)
        sql = f"call {self.proc}(); select pg_sleep(2);" \
              f"select username, object_name, detail_info from " \
              f"pg_query_audit((sysdate() - interval '20 seconds')" \
              f"::timestamp,sysdate()) where " \
              f"object_name = '{self.table}'and type = 'dml_action';"
        res = self.pri_sh.execut_db_sql(sql, dbname=self.db_name)
        self.log.info(res)
        self.assertIn(self.proc, res, f"执行失败:{text4}")
        self.assertIn(f"replace into {self.table} values(1, 'dbsec')",
                      res, f"执行失败:{text4}")
        self.assertIn(f"replace into {self.table} values(2, 'ccccc')",
                      res, f"执行失败:{text4}")

    def tearDown(self):
        text4 = '-----step4:清理环境 expect:成功-----'
        self.log.info(text4)
        drop_db_res = self.pri_sh.execut_db_sql(
            f"drop database if exists {self.db_name};"
            f"alter system set audit_dml_state = 0;"
            f"alter system set audit_dml_state_select = 0;")
        self.log.info(drop_db_res)
        self.assertIn(self.cons.DROP_DATABASE_SUCCESS, drop_db_res,
                      f"执行失败: {text4}")
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
